package com.bigModel.backend.controller.Code;

import com.bigModel.backend.advice.ResponseNotIntercept;
import com.bigModel.backend.advice.result.Result;
import com.bigModel.backend.pojo.DataType;
import com.bigModel.backend.service.Device.CodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

@ResponseNotIntercept
@RestController
@CrossOrigin(origins = "*")
public class CodeController {
    @Autowired
    private CodeService codeService;
    @PostMapping("/code/clean")
    public Result getCleaning(@RequestBody MultipartFile file,@RequestParam Integer method){
        if (file.isEmpty()) {
            return Result.failed("请上传文件");
        }
        DataType result = codeService.getCleaning(file,method);
        if (result==null){
            return Result.failed("没有找到文件，请匹配路径");
        }
        return Result.success(result);
    }

    @PostMapping("/code/dimreduction")
    public Result dimreduction(@RequestBody MultipartFile file,@RequestParam Integer method){
        if (file.isEmpty()) {
            return Result.failed("请上传文件");
        }
        DataType result = codeService.dimreduction(file,method);
        if (result==null){
            return Result.failed("没有找到文件，请匹配路径");
        }
        return Result.success(result);
    }

    @PostMapping("/code/fusion")
    public Result datafusion(@RequestParam("file1") MultipartFile file1,
                             @RequestParam("file2") MultipartFile file2,
                             @RequestParam("file3") MultipartFile file3) {
        // 处理文件上传逻辑
        if (file1.isEmpty()||file2.isEmpty()||file3.isEmpty()) {
            return Result.failed("请上传文件");
        }
        DataType datafusion = codeService.datafusion(file1, file2, file3);
        if (datafusion==null){
            return Result.failed("没有找到文件，请匹配路径");
        }
        return Result.success(datafusion);
    }

    @PostMapping("/code/estimation")
    public Result estimation(@RequestBody MultipartFile file){
        if (file.isEmpty()) {
            return Result.failed("请上传文件");
        }
        DataType estimation = codeService.estimation(file);
        if (estimation==null){
            return Result.failed("算法执行失败");
        }
        return Result.success(estimation);
    }

}
